package org.edu.modules.sxjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.edu.common.api.vo.Result;
import org.edu.modules.sxjy.entity.OssFile;
import org.edu.modules.sxjy.entity.VeApply;
import org.edu.modules.sxjy.entity.VeClassTeacher;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VePractice;
import org.edu.modules.sxjy.entity.VeSumup;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.mapper.VePracticeMapper;
import org.edu.modules.sxjy.mapper.VeSumupMapper;
import org.edu.modules.sxjy.oss.FileDTO;
import org.edu.modules.sxjy.service.IVeApplyService;
import org.edu.modules.sxjy.service.IVeClassTeacharService;
import org.edu.modules.sxjy.service.IVeCompanyUserService;
import org.edu.modules.sxjy.service.IVePracticeService;
import org.edu.modules.sxjy.service.IVeSumupService;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.VeSumupAddVo;
import org.edu.modules.sxjy.vo.VeSumupInfoVo;
import org.edu.modules.sxjy.vo.VeSumupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeSumupServiceImpl
        extends ServiceImpl<VeSumupMapper, VeSumup>
        implements IVeSumupService
{
    @Autowired
    private VeSumupMapper veSumupMapper;
    @Autowired
    private IVeSumupService veSumupService;
    @Autowired
    private IVePracticeService vePracticeService;
    @Autowired
    private VeCommonClient veCommonClient;
    @Autowired
    private IVeCompanyUserService veCompanyUserService;
    @Autowired
    private OssFileServiceImpl ossFileService;
    @Autowired
    private VePracticeMapper vePracticeMapper;
    @Autowired
    private IVeClassTeacharService veClassTeacharService;
    @Autowired
    private IVeApplyService veApplyService;

    public List<VeSumupVo> selectAll(VeSumupVo veSumupVo, Integer startLine, Integer pageSize, VeCommon veCommon)
    {
        List<VeSumupVo> veSumupVoList = new ArrayList();
        VeClassTeacher veClassTeacher;
        if (veCommon.getUserType().contains("0"))
        {
            veSumupVoList = this.veSumupMapper.selectAll(veSumupVo, startLine, pageSize, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (Iterator localIterator = veClassTeacherList.iterator(); localIterator.hasNext();)
            {
                veClassTeacher = (VeClassTeacher)localIterator.next();
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                veSumupVoList = this.veSumupMapper.selectAll(veSumupVo, startLine, pageSize, veCommon);
            } else {
                veSumupVoList = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                veSumupVoList = this.veSumupMapper.selectAll(veSumupVo, startLine, pageSize, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            VeCompanyUser veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            veSumupVoList = this.veSumupMapper.selectAll(veSumupVo, startLine, pageSize, veCommon);
        }
        Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
        Map stuMap;
        Integer id;
        if (stuResult != null)
        {
            stuMap = (Map)stuResult;
            if (stuMap.get("id") != null)
            {
                id = this.veSumupMapper.getIsSubmit(Integer.parseInt(stuMap.get("id").toString()));
                for (VeSumupVo sumupVo : veSumupVoList) {
                    if ((id != null) && (sumupVo != null) && (sumupVo.getStuUserId() == id.intValue()))
                    {
                        sumupVo.setIsSubmit("已提交");

                        sumupVo.setCreateTimes(DateUtils.getDateToString(sumupVo.getCreateTime()));
                    }
                    else
                    {
                        sumupVo.setIsSubmit("未提交");
                    }
                }
            }
        }
        for (VeSumupVo sumupVo : veSumupVoList)
        {
            VeApply veApply = this.veSumupMapper.getStuApplyByStuUserId(sumupVo.getStuUserId());
            if ((veApply != null) && (!"".equals(veApply))) {
                if (veApply.getType() == 2)
                {
                    VePractice practiceByStuUserId = this.vePracticeMapper.getPracticeByStuUserId(sumupVo.getStuUserId());
                    sumupVo.setName(practiceByStuUserId.getCpName());
                    sumupVo.setPostName(practiceByStuUserId.getPostName());
                    sumupVo.setStuIsFocusApper(2);
                }
                else if (veApply.getType() == 1)
                {
                    sumupVo.setStuIsFocusApper(1);
                }
            }
        }
        return veSumupVoList;
    }

    public int getSumPage(VeSumupVo veSumupVo, VeCommon veCommon)
    {
        int pageNum = 0;
        if (veCommon.getUserType().contains("0"))
        {
            pageNum = this.veSumupMapper.getSumPage(veSumupVo, veCommon);
        }
        else if (veCommon.getUserType().equals("1"))
        {
            List<Integer> stuBjIdList = new ArrayList();

            List<VeClassTeacher> veClassTeacherList = this.veClassTeacharService.getBanjiIdByTeacherUserId(veCommon.getUserId());
            for (VeClassTeacher veClassTeacher : veClassTeacherList) {
                if ((veClassTeacher != null) && (!"".equals(veClassTeacher))) {
                    stuBjIdList.add(Integer.valueOf(veClassTeacher.getBjId()));
                }
            }
            veCommon.setStuBjId(stuBjIdList);
            if ((stuBjIdList != null) && (stuBjIdList.size() > 0)) {
                pageNum = this.veSumupMapper.getSumPage(veSumupVo, veCommon);
            } else {
                pageNum = 0;
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veCommon.getUserId()).getResult();
            Map stuMap = (Map)stuResult;
            if (stuMap != null)
            {
                veCommon.setStuId(Integer.parseInt(stuMap.get("id").toString()));
                pageNum = this.veSumupMapper.getSumPage(veSumupVo, veCommon);
            }
        }
        else if ((veCommon.getUserType().equals("4")) || (veCommon.getUserType().equals("3")))
        {
            VeCompanyUser veCompanyUser = this.veCompanyUserService.getCpInfoByUserId(veCommon.getUserId());
            veCommon.setCpId(veCompanyUser.getCpId());

            pageNum = this.veSumupMapper.getSumPage(veSumupVo, veCommon);
        }
        return pageNum;
    }

    public VeSumupInfoVo getOneInfo(int id, String userType, String userid)
    {
        VePractice vePractice = (VePractice)this.vePracticeService.getById(Integer.valueOf(id));

        VeSumupInfoVo veSumupInfoVo = new VeSumupInfoVo();
        String cteacherUserName;
        if (vePractice != null)
        {
            veSumupInfoVo.setXh(vePractice.getXh());

            veSumupInfoVo.setXm(vePractice.getXm());

            veSumupInfoVo.setCpName(vePractice.getCpName());

            veSumupInfoVo.setPostName(vePractice.getPostName());

            String spec = this.veSumupMapper.getSpec(vePractice.getSpecId());
            if ((spec != null) && (!"".equals(spec))) {
                veSumupInfoVo.setSpec(spec);
            }
            veSumupInfoVo.setStartTime(DateUtils.getDateToString(vePractice.getStartTime()));
            if (vePractice.getBjId() != 0)
            {
                List<String> teacherNameList = this.veSumupMapper.getTeacherNameByBjId(vePractice.getBjId());
                if ((teacherNameList != null) && (teacherNameList.size() > 0)) {
                    veSumupInfoVo.setUserName(teacherNameList.toString().replace("[", "").replace("]", ""));
                }
            }
            cteacherUserName = this.veSumupMapper.getUserIdNameByUserId(vePractice.getCteacherUserId());
            if ((cteacherUserName != null) && (!"".equals(cteacherUserName))) {
                veSumupInfoVo.setCteacherUserName(cteacherUserName);
            }
        }
        List<VeSumup> veSumupList = this.veSumupService.list();
        for (VeSumup veSumup : veSumupList) {
            if (veSumup.getStuUserId() == vePractice.getStuUserId())
            {
                veSumupInfoVo.setContent(veSumup.getContent());

                veSumupInfoVo.setCreateTime(DateUtils.getDateToString(vePractice.getCreateTime()));
                if ((veSumup != null) && (!"".equals(veSumup)))
                {
                    String url = ((OssFile)this.ossFileService.getById(veSumup.getFileId())).getUrl();
                    veSumupInfoVo.setFile(url);
                }
            }
        }
        return veSumupInfoVo;
    }

    public Result<?> addSumup(VeSumupAddVo veSumupAddVo)
    {
        Object stuResult = this.veCommonClient.queryStudentById("09d5e1e7f9b049008eee645c783a1d67", veSumupAddVo.getUserId()).getResult();
        Map stuMap = (Map)stuResult;


        VeSumup veSumup = new VeSumup();
        if (stuMap.get("id") != null) {
            veSumup.setStuUserId(Integer.parseInt(stuMap.get("id").toString()));
        }
        veSumup.setContent(veSumupAddVo.getContent());

        veSumup.setCreateTime(System.currentTimeMillis());

        veSumup.setPrcId(veSumupAddVo.getId());
        if ((veSumupAddVo.getFileDTO() != null) && (!"".equals(veSumupAddVo.getFileDTO())))
        {
            OssFile ossFile = new OssFile();
            ossFile.setUrl(veSumupAddVo.getFileDTO().getWebUrl());
            this.ossFileService.save(ossFile);
            List<OssFile> ossFileList = this.ossFileService.list();
            for (OssFile file : ossFileList) {
                if ((file.getUrl() != null) && (!"".equals(file.getUrl())) &&
                        (file.getUrl().equals(veSumupAddVo.getFileDTO().getWebUrl()))) {
                    veSumup.setFileId(file.getId());
                }
            }
        }
        veSumup.setTerminalId(1);



        this.veSumupService.save(veSumup);



        return Result.OK("提交成功");
    }
}
